import 'package:backend.core/core/domain/entities/api/google_play/api_google_play_one_time_purchased_item.dart';
import 'package:equatable/equatable.dart';
import 'package:backend.purchases/infrastructure/dtos/api/google_play/api_google_play_one_time_purchased_item_transaction_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/google_play/api_google_play_purchased_item_acknowledgement_status_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/google_play/api_google_play_purchased_item_dto.dart';

const kApiGooglePlayOneTimePurchasedItemTransactionKey = 'transaction';
const kApiGooglePlayOneTimePurchasedItemRevokedKey = 'revoked';

const kApiGooglePlayOneTimePurchasedItemTypeValue = 'one_time_purchased_item';

class ApiGooglePlayOneTimePurchasedItemDto extends Equatable
    implements ApiGooglePlayPurchasedItemDto {
  const ApiGooglePlayOneTimePurchasedItemDto._internal({
    required this.purchaseItemSku,
    required this.purchaseToken,
    required this.test,
    required this.acknowledgementStatusDto,
    required this.revoked,
    required this.transactionDto,
  });

  @override
  final String purchaseItemSku;

  @override
  final String purchaseToken;

  @override
  final bool test;

  @override
  final ApiGooglePlayPurchasedItemAcknowledgementStatusDto acknowledgementStatusDto;

  final bool revoked;
  final ApiGooglePlayOneTimePurchasedItemTransactionDto transactionDto;

  @override
  String get type => kApiGooglePlayOneTimePurchasedItemTypeValue;

  @override
  List<Object?> get props {
    return [
      purchaseItemSku,
      purchaseToken,
      test,
      acknowledgementStatusDto,
      revoked,
      transactionDto,
    ];
  }

  factory ApiGooglePlayOneTimePurchasedItemDto.fromEntity(
      ApiGooglePlayOneTimePurchasedItem oneTimePurchasedItem) {
    final transactionDto = ApiGooglePlayOneTimePurchasedItemTransactionDto.fromEntity(
        oneTimePurchasedItem.transaction);

    final acknowledgementStatusDto = ApiGooglePlayPurchasedItemAcknowledgementStatusDto.fromEntity(
        oneTimePurchasedItem.acknowledgementStatus);

    return ApiGooglePlayOneTimePurchasedItemDto._internal(
      purchaseItemSku: oneTimePurchasedItem.purchaseItemSku,
      purchaseToken: oneTimePurchasedItem.purchaseToken,
      test: oneTimePurchasedItem.test,
      acknowledgementStatusDto: acknowledgementStatusDto,
      revoked: oneTimePurchasedItem.revoked,
      transactionDto: transactionDto,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      kApiGooglePlayPurchasedItemTypeKey: type,
      kApiGooglePlayPurchasedItemPurchaseItemSkuKey: purchaseItemSku,
      kApiGooglePlayPurchasedItemPurchaseTokenKey: purchaseToken,
      kApiGooglePlayPurchasedItemTestKey: test,
      kApiGooglePlayPurchasedItemAcknowledgementStatusKey: acknowledgementStatusDto.toString(),
      kApiGooglePlayOneTimePurchasedItemRevokedKey: revoked,
      kApiGooglePlayOneTimePurchasedItemTransactionKey: transactionDto.toJson(),
    };
  }
}
